package com.huaqin.develop.demotest.controller;

import com.huaqin.develop.demotest.pojo.WmsPurchaseDto;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/mq")
public class ProducerController {
    // 自动注入 RocketMQTemplate模板类，用于生产消息
    @Autowired
    private RocketMQTemplate mqTemplate;


    // 模拟生产者重复消费问题，前提是数据库没有唯一索引，并且项目未做幂等性校验
    @PostMapping("/send")
    public String testSend(@RequestBody WmsPurchaseDto params) {
        try {
            for (int i = 0; i < 1; i++) {
                mqTemplate.convertAndSend("fourbrothertopic", params);
            }
            int i = 1 / 0;
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


    @GetMapping("/send1")
    public String testSend1() {
        try {
            for (int i = 0; i < 1000; i++) {
                WmsPurchaseDto params = new WmsPurchaseDto(new Date().getTime(), "测试");
                mqTemplate.convertAndSend("fourbrothertopic", params);
            }
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }
}
